<template>
    <div class="app-container">
        <div class="warp_padding">  
            <div class="head-container">
                <!-- 搜索 -->
                <label id="labelQuery" class="labelQuery">老师姓名: </label>
                <el-input v-model="formDate.teaName" clearable placeholder="老师姓名搜索" style="width: 140px;" class="filter-item" @keyup.enter.native="toQuery" @change="toQuery()"/>
                
                <span v-if="device!=='mobile'">    
                    <label class="labelQuery">目标学校: </label>
                    <el-input v-model="formDate.newSchName" clearable placeholder="输入目标学校搜索" style="width: 160px;" class="filter-item" @keyup.enter.native="toQuery" @change="toQuery()"/>

                    <label class="labelQuery">调度状态: </label>
                    <el-select v-model="formDate.status" clearable placeholder="调度状态" @change="toQuery()" style="width:120px">
                        <el-option
                              v-for="item in statu"
                              :key="item.id"
                              :label="item.name"
                              :value="item.id">
                        </el-option>
                    </el-select>
                </span>

                <el-button class="filter-item" size="mini" type="primary" icon="el-icon-search" @click="toQuery">搜索</el-button>
                <!-- 新增 -->
                <div style="display: inline-block;margin: 0px 2px;">
                  <el-button
                    v-if="userId != 'admin'"
                    class="filter-item"
                    size="mini"
                    type="warning"
                    icon="el-icon-plus"
                    @click="add">调度申请</el-button>
                  <eForm ref="form" :is-add="isAdd" :teaList="teaList" :schList="schList"/>
                </div>
            </div>

            <!--列表-->
            <el-table :data="lists" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;" max-height="650">
                <el-table-column type="selection" width="30"></el-table-column>
                
                <el-table-column type="index" width="45"></el-table-column>
                
                <el-table-column prop="teaName" label="老师姓名" :show-overflow-tooltip="true"></el-table-column>

                <!-- <el-table-column prop="areaCode" label="县/区"  min-width="150" :show-overflow-tooltip="true"></el-table-column> -->

                <el-table-column prop="moblie" label="手机号"  min-width="100"></el-table-column>  

                <el-table-column prop="oldSchName" label="原来学校"  min-width="180" :show-overflow-tooltip="true"></el-table-column>  

                <el-table-column prop="newSchName" label="目标学校"  min-width="180" :show-overflow-tooltip="true"></el-table-column>           

                <el-table-column prop="status" label="状态"  class="staus">
                    <template slot-scope="scope">
                        <el-tag :type="scope.row.status | statusFilter">{{scope.row.status | formatStata}}</el-tag>
                    </template>
                </el-table-column>

                 <el-table-column prop="createtime" label="创建时间" sortable  min-width="100" >
                    <template slot-scope="scope">
                      <span v-if="scope.row.createtime">{{ scope.row.createtime | formatDate }}</span>
                    </template>
                </el-table-column>

                <el-table-column label="操作" width="250">
                    <template slot-scope="scope">
                        <el-button size="mini" type="primary"  v-if="userId != 'admin' && scope.row.status =='2'"  @click="handleAdu(scope.$index, scope.row.id)">重新申请</el-button>
                        <el-button size="mini" type="success"  v-if="userId != 'admin' && scope.row.status !='1'"  @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                        <el-button type="danger" size="mini" v-if="userId != 'admin' && scope.row.status !='1'" @click="handleDel(scope.$index, scope.row.id)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>

        <!--工具条-->
        <el-col :span="24" class="toolbar">
            <el-button type="danger" size="mini" @click="batchRemove" :disabled="this.sels.length===0" v-if="userId != 'admin'" class="remove">批量删除</el-button>

            <el-pagination background  @size-change="handleSizeChange"  @current-change="handleCurrentChange"  :current-page.sync="formDate.pageNo" :page-count="pageCount" :page-sizes="[10, 20, 30, 50,100]"  layout="total, sizes,prev, pager, next, jumper" :pager-count="pagerCount" :total="total" class="pagination"></el-pagination>
        </el-col>

    </div>
</template>

<script>



import checkPermission from '@/utils/permission' 
import {formatDate} from '@/utils/formatDate.js';
import eForm from './component/form'
import { dispatchList, dispatchDel, readyDispatch, dispatchAudit } from '@/api/request'
import { mapGetters } from 'vuex'
export default {
    components: { eForm },
    computed: {
        ...mapGetters([
          'device',
          'userId'
        ])
    },
    data() {
        return {
            teaList:[],
            schList:[],
            schId:'',
            pageCount: 0,
            total:0,  
            listLoading: false,
            lists: [],
            sels: [],//列表选中列
            pagerCount:11,   
            formDate:{
                pageNo: 1,  
                pageSize:10,
                teaName:'',
                status:'',
                type:0,
            },
            isAdd:true,
            statu: [
                {
                    id: 0,
                    name: '审核中'
                },
                {
                    id: 1,
                    name: '已通过'
                },
                {
                    id: 2,
                    name: '不通过'
                },
                
            ],
         }
    },
    methods: {
        checkPermission,
        status(row, column) {
            return row.status == 0 ? '审核中' : row.status == 1 ? '审核通过' : row.status == 2 ? '不通过':'未知';
        },
        selsChange: function (sels) {
                this.sels = sels;
        },
        handleCurrentChange(val) {
            this.formDate.pageNo = val;
            this.init()
        },
        handleSizeChange(val) {
            this.formDate.pageSize = val
            this.init()
            // console.log(`page ${val}`);
        },
        toQuery(){
            this.init()
        },
        add(){
            this.ready()
            this.isAdd = true
            this.$refs.form.form = { 
                id:'',
                oldSchId:this.schId,
                teaId:'',
                newSchId:'',
                dispatchInfo:'',
                status:0,
            },
            this.$refs.form.dialog = true
        },
        handleAdu(index,ID){
            let params ={
                status:0,
                id:ID
            }
            dispatchAudit(params).then(res => {
                this.$notify({
                    title: '提交成功',
                    type: 'success',
                    duration: 2500
                })
                this.init()
            })
        },
        init(){
            this.listLoading = true
            let params = {...this.formDate}
            dispatchList(params).then(res => { 
                let { list, total } = res
                for(let i=0;i<list.length;i++){
                   if (list[i].status) {
                      (list[i].status).toString()
                   }                   
                }
                this.lists = list
                this.total = total
                this.listLoading = false 
            })
        },
        handleEdit(index, row) {
            this.ready()
            this.isAdd = false
            this.$refs.form.dialog = true
            let Resuts = Object.assign({}, row) 
            this.$refs.form.form = Resuts
        },
        batchRemove(){
            let ids = this.sels.map(item => item.id);
            this.$confirm('确认删除选中的记录吗?', '提示', {
                        type: 'warning'
                    }).then(() => {
                        this.listLoading = true;
                        dispatchDel(ids).then(res => {
                            this.listLoading = false;
                            this.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                            if (this.lists.length == 1 && this.formDate.pageNo != 1) 
                            this.formDate.pageNo--
                            this.init();
                        })

                    }).catch(() => {
            });
        },
        handleDel(index,ID){
            let ids = [];
            ids.unshift(ID)
            this.$confirm('确认删除该记录吗?', '提示', {
                        type: 'warning'
                    }).then(() => {
                        // console.log(ids)
                        this.listLoading = true;
                        dispatchDel(ids).then(res => {
                            this.listLoading = false;
                            this.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                            if (this.lists.length == 1 && this.formDate.pageNo != 1) 
                            this.formDate.pageNo--
                            this.init();
                        })

                    }).catch(() => {
            });
        },
        async ready(){
            try {
                let res = await readyDispatch()
                this.schList = res.data.schList
                this.teaList = res.data.teaList
                this.schId  = res.data.schId
            } catch (e) {
                console.log(e)
            }

        },

        
    },
    mounted:function(){
        this.init()
    },
    filters: {
        formatDate(time) {
            let date = new Date(time);
            return formatDate(date, 'yyyy-MM-dd');
        }, 
        // el-tag类型转换
        statusFilter(status) {
            const statusMap = {
                0: 'success',
                1: 'info',
                2: 'danger',
            }
            return statusMap[status]
        },
       // 状态显示转换
        formatStata(status) {
            const statusMap = {
                0: '审核中',
                1: '已通过',
                2: '不通过',
            }
            return statusMap[status]
        }
    },
}

</script>


<style scoped lang="scss">

</style>
